import { Component, ViewContainerRef, ComponentRef, EventEmitter, Injector, Input, Output, ViewChild, ComponentFactoryResolver, OnDestroy } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { WorkflowServiceProxy, OpenUserActionDto, OutsourcingWorkFlowServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';

import { OrdinaryOutsourcingDetailComponent } from '../OrdinaryOutsourcingDetail/OrdinaryOutsourcingDetail.component';
import { OrdinaryCrucialDetailComponent } from '../OrdinaryCrucialDetail/OrdinaryCrucialDetail.component';
import { ProcessingOrderDetailComponent } from '../ProcessingOrderDetail/ProcessingOrderDetail.component';
import { ProcessingDeliveryDetailComponent } from '../ProcessingDeliveryDetail/ProcessingDeliveryDetail.component';
import { OutsourcingApplyChangeDetailComponent } from '../OutsourcingApplyChangeDetail/OutsourcingApplyChangeDetail.component';

import { OrdinaryCrucialAuditComponent } from '../OrdinaryCrucialAudit/OrdinaryCrucialAudit.component';
import { OrdinaryOutsourcingAuditComponent } from '../OrdinaryOutsourcingAudit/OrdinaryOutsourcingAudit.component';
import { ProcessingOrderAuditComponent } from '../ProcessingOrderAudit/ProcessingOrderAudit.component';
import { ProcessingDeliveryAuditComponent } from '../ProcessingDeliveryAudit/ProcessingDeliveryAudit.component';
import { ProcessingContentApplyChangeDetailComponent } from '../ProcessingContentApplyChangeDetail/ProcessingContentApplyChangeDetail.component';


@Component({
  selector: 'flowworkauditdetailmodel',
  templateUrl: './flowworkauditDetailModel.component.html',
  styleUrls: ['./flowworkauditDetailModel.component.css']
})
export class FlowworkauditDetailModelComponent extends AppComponentBase {
  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild("alertcontainer", { read: ViewContainerRef }) container: ViewContainerRef;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  @Input() component: any;     //需要加载的组件名

  comps: any;
  active = false;
  componentRef: ComponentRef<any>;

  constructor(injector: Injector,
    private resolver: ComponentFactoryResolver) {
    super(injector);
  }

  createComponent(workflowid: string) {
    //this.container.clear();
    console.log(this.component);

    // tslint:disable-next-line: triple-equals
    if (this.component == '一般过程外协申请') {
      this.comps = OrdinaryOutsourcingDetailComponent;
    // tslint:disable-next-line: triple-equals
    } else if (this.component == '关键过程外协申请') {
      this.comps = OrdinaryCrucialDetailComponent;
      // tslint:disable-next-line: triple-equals
    } else if (this.component == '一般过程外包加工申请' || this.component == '关键过程外包加工申请') {
      this.comps = ProcessingOrderDetailComponent;
      // tslint:disable-next-line: triple-equals
    } else if (this.component == '外包项目确认单') {
      this.comps = ProcessingDeliveryDetailComponent;
    // tslint:disable-next-line: triple-equals
    } else if (this.component == '一般过程外包申请变更通知单' || this.component == '关键过程外包申请变更通知单') {
      this.comps = OutsourcingApplyChangeDetailComponent;
    // tslint:disable-next-line: triple-equals
    } else if (this.component == '一般过程外包加工内容变更通知单' || this.component == '关键过程外包加工内容变更通知单') {
      this.comps = ProcessingContentApplyChangeDetailComponent;
    }

    const factory = this.resolver.resolveComponentFactory(this.comps);
    factory.inputs.push({ propName: 'workflowid', templateName: 'ss' });
    this.componentRef = this.container.createComponent(factory);
    this.componentRef.instance['workflowid'] = workflowid;
    this.componentRef.instance.out.subscribe((res) => {
      this.close();
    });
  }

  callBack(evt) {
    //回调参数
    const param = evt.param;
    this.notify.info(this.l('SavedSuccessfully'));
    console.log(param);
    console.log(this.modal);
  }

  show(workflowName: string, workflowid: string): void {
    this.active = true;
    this.component = workflowName;
    setTimeout(() => this.createComponent(workflowid), 50);

    //this.createComponent( workflowid);
    this.modal.show();
  }

  close(): void {
    this.ngOnDestroy();
    this.active = false;
    this.modal.hide();
    this.modalSave.emit(null);
  }

  // tslint:disable-next-line: use-life-cycle-interface
  ngOnDestroy() {
    console.log(this.componentRef);
    if (this.componentRef) {
      this.componentRef.destroy();
    }
  }
}
